import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/User.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
// 全局样式
import '../assets/css/global.css'
// 引入字体和图标
import '../assets/fonts/iconfont.css'

import axios from 'axios'
// 配置请求的根路径
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
axios.interceptors.request.use(config =>{
  config.headers.Authorization = window.sessionStorage.getItem('token')
  // 固定写法，在最后return config
  return config
})
Vue.prototype.$http = axios

Vue.use(VueRouter)


const routes = [
  
]

const router = new VueRouter({
  routes:[
    { path: '/', redirect: '/login' },// 重定向到login
    { path: '/login', component: Login },
    { 
      path: '/home', 
      component: Home ,
      redirect: '/welcome',
      children:[
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users},
        { path: '/rights', component: Rights},
        { path: '/roles', component: Roles},
    
      ]
    },
    
  ]
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to 将于访问的路径
  //from 从哪个路径跳转而来
  //next 是一个函数，表示放行
  //      next()  放行       next('/login')
  if(to.path === '/login') return next();
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if(!tokenStr) return next('/login')
  next()
})

export default router
